package com.cmccgz.udacs_landlord_webapp.main.warning.presenters;


import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.bean.WarningDetail;
import com.cmccgz.udacs_landlord_webapp.main.warning.contracts.WarningDetailContract;

import rx.Subscriber;

public class WarningDetailPresenter implements WarningDetailContract.Presenter {
    private WarningDetailContract.View view;

    public WarningDetailPresenter(WarningDetailContract.View view) {
        this.view = view;
    }

    @Override
    public void getDetail(String w_id) {
        ApiService.getWarningDetail(w_id).subscribe(new Subscriber<ResultBean<WarningDetail>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                view.getDetailError("获取失败！");

            }

            @Override
            public void onNext(ResultBean<WarningDetail> warningDetailResultBean) {
                if ("0".equals(warningDetailResultBean.rsmsg.rscode) && warningDetailResultBean.resp != null) {
                    view.getDetailSuccess(warningDetailResultBean.resp);
                } else {
                    if (!StringUtils.isEmpty(warningDetailResultBean.rsmsg.rsdesc))
                        view.getDetailError(warningDetailResultBean.rsmsg.rsdesc);
                    else
                        onError(null);
                }
            }
        });
     /*   final WarningDetail detail = new WarningDetail();
        detail.house_name="南州街西栋1024号";
        detail.w_state=Integer.parseInt(w_id)%4+1+"";
        detail.desc="开不了门，赶快过来修，谢谢！";
        detail.c_man="李健";
        detail.c_phone="10086";
        detail.c_timestamp=new Date().getTime()+"";
        detail.d_timestamp=new Date().getTime()+"";
        detail.d_man="你妹";
        detail.d_phone="5555";
        detail.d_result="修不好";
        detail.d_type="0";
        detail.device_id="1";
        detail.device_name="设备10086";
        detail.media=new ArrayList<>();
        detail.media.add(new Media("a6477735-e793-4ce5-b90d-96f228d078e4"));
        detail.media.add(new Media("a6477735-e793-4ce5-b90d-96f228d078e4"));
        detail.media.add(new Media("a6477735-e793-4ce5-b90d-96f228d078e4"));
        detail.media.add(new Media("a6477735-e793-4ce5-b90d-96f228d078e4"));
        if (view instanceof BaseFragment) {
            ((BaseFragment) view).getView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.getDetailSuccess(detail);
                }
            },1000);
        }*/
    }

    @Override
    public void deleWarning(String w_id) {
        ApiService.deleWarning(w_id).subscribe(new Subscriber<ResultBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                view.deleError("删除失败!");
            }

            @Override
            public void onNext(ResultBean resultBean) {
                if ("0".equals(resultBean.rsmsg.rscode) ) {
                    view.deleSuccess();
                } else {
                    view.deleError(resultBean.rsmsg.rsdesc);
                }
            }
        });
    }

    @Override
    public void confirmWarning(String w_id) {
        ApiService.confirmWarning(w_id).subscribe(new Subscriber<ResultBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                view.confirmError("确认失败!");
            }

            @Override
            public void onNext(ResultBean resultBean) {
                if ("0".equals(resultBean.rsmsg.rscode) ) {
                    view.confirmSuccess();
                } else {
                    view.confirmError(resultBean.rsmsg.rsdesc);
                }
            }
        });
    }
}
